package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * TODO: 增加描述
 *
 * @author li.xh
 * @version 1.0.0
 * @date 2019-12-10 20:06
 * @copyright wonhigh.cn
 */
@RestController
@RequestMapping("/security")
public class SecurityController {

    @RequestMapping("/checkItem")
    public Map checkitem() {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Collection<GrantedAuthority> authorities = user.getAuthorities();
        if (authorities == null && authorities.size() <= 0){
            return null;
        }
        Map<String,Boolean> map = new HashMap<>();
        for (GrantedAuthority authority : authorities) {
            if ("CHECKITEM_ADD".equals(authority.getAuthority())){
                map.put("add",true);
                continue;
            }
            if ("CHECKITEM_EDIT".equals(authority.getAuthority())){
                map.put("edit",true);
                continue;
            }
            if ("CHECKITEM_DELETE".equals(authority.getAuthority())){
                map.put("delete",true);
                continue;
            }
        }
        return map;
    }

    @RequestMapping("/permission")
    public Map permission() {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Collection<GrantedAuthority> authorities = user.getAuthorities();
        if (authorities == null && authorities.size() <= 0){
            return null;
        }
        Map<String,Boolean> map = new HashMap<>();
        for (GrantedAuthority authority : authorities) {
            if ("PERMISSION_ADD".equals(authority.getAuthority())){
                map.put("add",true);
                continue;
            }
            if ("PERMISSION_EDIT".equals(authority.getAuthority())){
                map.put("edit",true);
                continue;
            }
            if ("PERMISSION_DELETE".equals(authority.getAuthority())){
                map.put("delete",true);
                continue;
            }
            if ("PERMISSION_QUERY".equals(authority.getAuthority())){
                map.put("query",true);
                continue;
            }
        }
        return map;
    }

    @RequestMapping("/checkGroup")
    public Map checkgroup() {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Collection<GrantedAuthority> authorities = user.getAuthorities();
        if (authorities == null && authorities.size() <= 0){
            return null;
        }
        Map<String,Boolean> map = new HashMap<>();
        for (GrantedAuthority authority : authorities) {
            if ("CHECKGROUP_ADD".equals(authority.getAuthority())){
                map.put("add",true);
                continue;
            }
            if ("CHECKGROUP_EDIT".equals(authority.getAuthority())){
                map.put("edit",true);
                continue;
            }
            if ("CHECKGROUP_DELETE".equals(authority.getAuthority())){
                map.put("delete",true);
                continue;
            }
        }
        return map;
    }


    @RequestMapping("/setmeal")
    public Map setmeal() {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Collection<GrantedAuthority> authorities = user.getAuthorities();
        if (authorities == null && authorities.size() <= 0){
            return null;
        }
        Map<String,Boolean> map = new HashMap<>();
        for (GrantedAuthority authority : authorities) {
            if ("SETMEAL_ADD".equals(authority.getAuthority())){
                map.put("add",true);
                continue;
            }
            if ("SETMEAL_EDIT".equals(authority.getAuthority())){
                map.put("edit",true);
                continue;
            }
            if ("SETMEAL_DELETE".equals(authority.getAuthority())){
                map.put("delete",true);
                continue;
            }
        }
        return map;
    }
}
